﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Microsoft.VisualStudio.TextTemplating;
namespace CodeGeneratorT4
{

    public class dd
    {
        private bool _state = false;
        private int temp = 0;
        List<DbColumn> tables = null;
        public dd()
        {


        }
        public dd(int i)
        {
            temp = i;
        }

        public void ford()
        {
            for (int i = 0; i < temp; i++)
            {
                //  WriteLine("This is  " + i + " test");
            }
        }
    }

    //public class c
    //{
    //    public c()
    //    {
    //        dd dd = new dd();
    //        dd = new dd(1);
    //    }


    //}

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow
    {


        public MainWindow()
        {
            InitializeComponent();
        }
        //https://www.cnblogs.com/hjf1223/archive/2010/09/05/t4_runtime_parameter.html
        //https://msdn.microsoft.com/en-us/library/bb126542.aspx
        private void ExecuteButton_Click(object sender, RoutedEventArgs e)
        {
            //string fileName = @"D:\Template\Model.tt";
            //   string fileName = System.AppDomain.CurrentDomain.BaseDirectory + @"\Template\Model.tt";
            string fileName = FilePathText.Text;



            if (!File.Exists(fileName))
            {
                ShowMessage("找不到文件");
                return;
            }

            if (TablesComboBox.SelectedValue != null)
            {
                string tableName = TablesComboBox.SelectedValue.ToString();
                List<DbColumn> tables = DbHelper.GetDbColumns(ConnectionStringText.Text, tableName);
                if (tables != null && tables.Count > 0)
                {
                    try
                    {
                        CustomCmdLineHost host = new CustomCmdLineHost();
                        Engine engine = new Engine();
                        host.TemplateFileValue = fileName;

                        //string input = "<#   temp=6;#>  ";
                        //input += File.ReadAllText(fileName);

                        string input = File.ReadAllText(fileName);
                        //  input += "<#   temp=6;#>  ";

                        input = input.Replace("int temp=1;", "int temp=6;");

                        string output = engine.ProcessTemplate(input, host);
                        string outputFileName = Path.GetFileNameWithoutExtension(fileName);
                        // ReSharper disable once AssignNullToNotNullAttribute
                        outputFileName = Path.Combine(Path.GetDirectoryName(fileName), outputFileName);
                        outputFileName = outputFileName + host.FileExtension;
                        File.WriteAllText(outputFileName, output, host.FileEncoding);

                        //获取错误
                        string error = host.Errors.Cast<CompilerError>().Aggregate("", (current, item) => current + item.ToString());
                        if (error.Length > 0)
                        {
                            ShowMessage(error);
                        }
                        else
                            ShowMessage("成功");
                    }
                    catch (Exception ex)
                    {
                        ShowMessage(ex.Message);
                    }
                }
                else
                {
                    ShowMessage("没查询到表,请确定表和数据库连接正确。");
                }
            }
            else
            {
                ShowMessage("请选择表");
            }



        }

        private void ShowMessage(string message)
        {
            MessageBox.Show(message);
        }

        private void FileSelectButton_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                //Filter = "Excel Files (*.tt)|*.tt|*"
                Filter = "t4 files (*.tt)|*.tt|All files (*.*)|*.*"
            }
            ;
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                FilePathText.Text = openFileDialog.FileName;
            }
        }
        /// <summary>
        /// 测试数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestBtn_Click(object sender, RoutedEventArgs e)
        {
            if (ConnectionStringText.Text.Length > 0)
            {
                try
                {
                    List<DbTable> tables = DbHelper.GetDbTables(ConnectionStringText.Text);

                    TablesComboBox.ItemsSource = tables;
                    ShowMessage("成功");
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }

            }
            else
            {
                ShowMessage("请输入连接字符串");

            }

        }
    }
}
